import { type BackgroundSetting, type LocalBackgroundSetting, newBackgroundSetting, newLocalBackgroundSetting } from '@/api/types/Setting.ts'
import type { PersistencePluginOption } from '@/config/store/PersistencePlugin.ts'
import { defineStore } from 'pinia'
import { computed, type ComputedRef, type Ref, ref } from 'vue'

export const useBackgroundSettingStore = defineStore(
        '背景设置',
        () => {
            const setting: Ref<BackgroundSetting> = ref(newBackgroundSetting())
            const localSetting: Ref<LocalBackgroundSetting> = ref(newLocalBackgroundSetting())

            const currentSetting: ComputedRef<BackgroundSetting> = computed(() =>
                    localSetting.value.enable
                            ? localSetting.value
                            : setting.value,
            )

            const currentSpecialEffects = computed(() => currentSetting.value.specialEffects)

            return {
                setting,
                localSetting,
                currentSetting,
                currentSpecialEffects,
                setValue: (value: BackgroundSetting) => setting.value = value,
                setLocalValue: (value: LocalBackgroundSetting) => localSetting.value = value,
            }
        },
        {
            persistencePlugin: {
                persistence: true,
                eternal: true,
            } as PersistencePluginOption,
        },
)
